package com.mc.cloud.mall.cartOrder.service;

import com.mc.cloud.mall.cartOrder.feign.ProductFeignClient;
import com.mc.cloud.mall.cartOrder.model.mapper.CartMapper;
import com.mc.cloud.mall.cartOrder.model.pojo.Cart;
import com.mc.cloud.mall.cartOrder.model.vo.CartVO;
import com.mc.cloud.mall.categoryProduct.model.mapper.ProductMapper;
import com.mc.cloud.mall.categoryProduct.model.pojo.Product;
import com.mc.cloud.mall.common.common.Constant;
import com.mc.cloud.mall.common.exception.MallException;
import com.mc.cloud.mall.common.exception.MallExceptionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @ClassName CartServiceImpl
 * @Author MC
 * @Date 2020/11/6 9:01
 * @Version 1.0
 **/
@Service
public class CartServiceImpl implements CartService {
    
    @Autowired
    CartMapper cartMapper;
    
    @Autowired
    ProductFeignClient productFeignClient;
    
    
    @Override
    public List<CartVO> add(Integer productId, Integer userId, Integer count){
        //验证商品是否合法
        validProduct(productId,count);
        
        //添加购物车  1 在购物车，只需要添加数量  2 不在购物车 新增商品
        Cart cart = cartMapper.selectByProductIdAndUserId(productId, userId);
        if (cart ==null){
            cart = new Cart();
            cart.setProductId(productId);
            cart.setUserId(userId);
            cart.setQuantity(count);
            cart.setSelected(Constant.Cart.CHECKED);
            cartMapper.insertSelective(cart);
        }else{
            count = cart.getQuantity() + count;
            Cart cartNew = new Cart();
            cartNew.setUserId(cart.getUserId());
            cartNew.setId(cart.getId());
            cartNew.setQuantity(count);
            cartNew.setProductId(productId);
            cartMapper.updateByPrimaryKeySelective(cartNew);
        }
        System.out.println(cart);
        return this.cartList(cart.getUserId());
    
    }
    
    private void validProduct(Integer productId, Integer count) {
        //查询出商品
        Product product = productFeignClient.productDetailForFeign(productId);
        //看商品是否存在，以及是否上架
        if (product==null && product.getStatus().equals(Constant.SellStatus.NOT_SALE)){
            throw new MallException(MallExceptionEnum.NOT_SALE);
        }
        //是否超出库存
        if (product.getStock()<count){
            throw new MallException(MallExceptionEnum.NOT_ENOUGH);
        }
    
    }
    
    @Override
    public List<CartVO> cartList(Integer userId){
        List<CartVO> cartVOS = cartMapper.selectList(userId);
        for (CartVO cartVO : cartVOS) {
            
            cartVO.setTotalPrice(cartVO.getPrice()*cartVO.getQuantity());
        }
        System.out.println(cartVOS);
        return cartVOS;
    }
    
    @Override
    public List<CartVO> update(Integer productId, Integer userId, Integer count){
        //验证商品是否合法
        validProduct(productId,count);
        
        //更新购物车
        Cart cart = cartMapper.selectByProductIdAndUserId(productId, userId);
        if (cart ==null){
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }else{
            Cart cartNew = new Cart();
            cartNew.setUserId(cart.getUserId());
            cartNew.setId(cart.getId());
            cartNew.setQuantity(count);
            cartNew.setProductId(productId);
            cartMapper.updateByPrimaryKeySelective(cartNew);
        }
        return this.cartList(userId);
        
    }
    
    
    @Override
    public List<CartVO> delete(Integer productId, Integer userId){
        
        //删除购物车
        Cart cart = cartMapper.selectByProductIdAndUserId(productId, userId);
        if (cart ==null){
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }else{
            cartMapper.deleteByPrimaryKey(cart.getId());
        }
        return this.cartList(userId);
        
    }
    
    @Override
    public List<CartVO> select(Integer userId, Integer productId, Integer selected){
        
        Cart cart = cartMapper.selectByProductIdAndUserId(productId, userId);
        if (cart ==null){
            throw new MallException(MallExceptionEnum.UPDATE_SELECTED_FAILED);
        }else{
            cartMapper.select(userId, productId, selected);
        }
        return this.cartList(userId);
        
    }
    
    @Override
    public List<CartVO> selectAll(Integer userId,Integer selected){
        
        cartMapper.select(userId,null,selected);
        return this.cartList(userId);
        
    }
}
